package com.itheima.web.service;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.web.domain.Admin;
import com.itheima.web.domain.Result;
import com.itheima.web.mapper.AdminMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @author 阿鹏
 * @date 2021/3/2 10:33
 */
@Service
public class AdminService extends ServiceImpl<AdminMapper, Admin> {


    public ResponseEntity login(Admin admin) {
        Admin administrator = query().eq("username", admin.getUsername()).one();
        if (administrator == null) {
            return ResponseEntity.status(401).body(new Result(false, "权限不足"));
        }

        if (!StringUtils.equals(SecureUtil.md5(admin.getPassword()), administrator.getPassword())) {
            return ResponseEntity.status(500).body(new Result(false, "密码错误"));
        }

        return ResponseEntity.ok(new Result(true, "登录成功"));
    }
}
